<template>
  <el-dialog
    :model-value="modelValue"
    @update:model-value="changeModelValue"
    title="请选择商品类型"
    width="800px"
    :close-on-click-modal="false"
    :append-to-body="true"
    top="50px"
  >
    <div class="type_box">
      <el-form ref="formRef" :model="form" style="width: 100%">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="类型名称" prop="name">
              <el-input
                v-model="form.name"
                style="width: 100%"
                placeholder="请输入名称"
                @keyup.enter="search"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="" prop="name">
              <el-button :icon="Search" type="primary" @click="search">查 询</el-button>
              <el-button :icon="Refresh" @click="reset">重 置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- <div class="alert">双击行选中数据！</div> -->
      <el-table :data="tableData" style="width: 100%" border @row-dblclick="confirm">
        <el-table-column label="" align="center" width="80px">
          <template #default="{ row }">
            <el-radio v-model="currentTypeId" :value="row.id"></el-radio>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="类型名称" align="center"></el-table-column>
        <el-table-column prop="total" label="商品总数" align="center"></el-table-column>
        <el-table-column prop="memo" label="备注" align="center">
          <template #default="{ row }">
            {{ row.memo || '---' }}
          </template>
        </el-table-column>
      </el-table>
      &nbsp;
      <el-pagination
        style="display: flex; justify-content: flex-end"
        background
        :current-page="pagenation.page"
        :page-size="pagenation.limit"
        :total="pagenation.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="changeModelValue(false)">取 消</el-button>
        <el-button type="primary" @click="confirm"> 确 认 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
  import { ref, onMounted, watch } from 'vue';
  import { getTypePage } from '@/server/type';
  import { Search, Refresh, Plus, EditPen, Delete } from '@element-plus/icons-vue';

  const props = defineProps({
    modelValue: {
      type: Boolean,
      default: false,
    },
    rowId: {
      type: Number || String,
      default: null,
    },
  });
  const emit = defineEmits(['update:modelValue', 'callback']);

  const currentType = ref({});
  const currentTypeId = ref();

  const form = ref({
    name: '',
  });
  const pagenation = ref({
    page: 1,
    limit: 10,
    total: 0,
  });

  const tableData = ref([]);

  const handleSizeChange = size => {
    pagenation.value.limit = size;
    loadData();
  };
  const handleCurrentChange = page => {
    pagenation.value.page = page;
    loadData();
  };

  watch(
    () => props.modelValue,
    v => {
      if (v) {
        currentTypeId.value = props.rowId;
        pagenation.value.page = 1;
        pagenation.value.limit = 10;
        form.value.name = '';
        loadData();
      }
    },
  );

  function confirm(val) {
    currentType.value = tableData.value.find(item => item.id === currentTypeId.value);
    emit('callback', currentType.value);
    emit('update:modelValue', false);
  }

  function search() {
    loadData();
  }
  function reset() {
    form.value.name = '';
    loadData();
  }

  async function loadData() {
    const searchModel = { ...form.value, ...pagenation.value };
    const res = await getTypePage(searchModel);
    if (res.code === 200) {
      tableData.value = res.data.rows;
      pagenation.value.total = res.data.total;
    }
  }

  function changeModelValue(val) {
    emit('update:modelValue', val);
    currentTypeId.value = null;
  }

  onMounted(() => {
    console.log('onMounted');
    loadData();
  });
</script>

<style lang="scss" scoped>
  .alert {
    font-size: 14px;
    color: red;
    padding-bottom: 12px;
  }
  .type_box {
    margin: 10px 0 20px;
  }
</style>
